﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using LinQProject.Interfaces;

namespace LinQData.Providers
{
    /// <summary>
    /// Responsible of instantiating each provider
    /// and cahing it into a Dictionary
    /// </summary>
    internal static class ProviderFactory
    {
        //Static providers cache
        static IDictionary<string, IPropertyMappingProvider> providers = new Dictionary<string, IPropertyMappingProvider>();

        public static IPropertyMappingProvider CreatePropertyMappingProvider(Type providerType)
        {
            IPropertyMappingProvider provider;

            //Check if the provider already exists in the cahce
            if (providers.ContainsKey(providerType.ToString()))
                provider = providers[providerType.ToString()];
            else
            {
                //Instaniate a new provider and add it to the cache
                provider = AppDomain.CurrentDomain
                                    .CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().ToString(), providerType.ToString()) as IPropertyMappingProvider;
                if (provider != null) providers.Add(provider.GetType().ToString(), provider);
            }
            return provider;
        }
    }
}
